using Firebase.Analytics;
using GoogleMobileAds.Api;
using System;
using UnityEngine;

public class AdManagerOld : MonoBehaviour
{
	private bool rewardedVideoAd_handler_registered;

	private readonly string appId = "ca-app-pub-3518148671029894~6333413211";

	private readonly string banner_ad_unit_id = "ca-app-pub-3518148671029894/9970567019";

	private readonly string rewarded_video_ad_unit_id = "ca-app-pub-3518148671029894/7178046437";

	private readonly string interstitial_ad_unit_id = "ca-app-pub-3518148671029894/4445616470";

	private BannerView bannerView;

	private InterstitialAd interstitial;

	private RewardBasedVideoAd rewardBasedAdmobVideo;

	public static AdManagerOld instance;

	public static AdManagerOld Instance
	{
		get
		{
			if (instance == null)
			{
				instance = (UnityEngine.Object.FindObjectOfType(typeof(AdManagerOld)) as AdManagerOld);
			}
			return instance;
		}
		set
		{
			instance = value;
		}
	}

	private void Awake()
	{
		if (instance != null && instance != this)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		instance = this;
	}

	private void Start()
	{
		MobileAds.Initialize(appId);
		RequestAdmobBanner();
		RequestInterstitial();
		rewardBasedAdmobVideo = RewardBasedVideoAd.Instance;
		if (!rewardedVideoAd_handler_registered)
		{
			rewardBasedAdmobVideo.OnAdLoaded += HandleAdmobRewardBasedVideoLoadedAdmob;
			rewardBasedAdmobVideo.OnAdFailedToLoad += HandleAdmobRewardBasedVideoFailedToLoadAdmob;
			rewardBasedAdmobVideo.OnAdOpening += HandleAdmobRewardBasedVideoOpenedAdmob;
			rewardBasedAdmobVideo.OnAdStarted += HandleAdmobRewardBasedVideoStartedAdmob;
			rewardBasedAdmobVideo.OnAdRewarded += HandleAdmobRewardBasedVideoRewardedAdmob;
			rewardBasedAdmobVideo.OnAdClosed += HandleAdmobRewardBasedVideoClosedAdmob;
			rewardBasedAdmobVideo.OnAdLeavingApplication += HandleAdmobRewardBasedVideoLeftApplicationAdmob;
			rewardedVideoAd_handler_registered = true;
		}
		RequestRewardVideo();
	}

	private void RequestAdmobBanner()
	{
		if (bannerView != null)
		{
			bannerView.Destroy();
		}
		bannerView = new BannerView(banner_ad_unit_id, AdSize.Banner, AdPosition.Bottom);
		bannerView.OnAdLoaded += HandleAdmobAdLoaded;
		bannerView.OnAdFailedToLoad += HandleAdmobAdFailedToLoad;
		bannerView.OnAdOpening += HandleAdmobAdOpened;
		bannerView.OnAdClosed += HandleAdmobAdClosed;
		bannerView.OnAdLeavingApplication += HandleAdmobAdLeftApplication;
		bannerView.LoadAd(CreateAdmobAdRequest());
		bannerView.Show();
	}

	public void ShowAdmobBanner()
	{
		if (bannerView != null)
		{
			bannerView.Show();
		}
	}

	public void HideAdmobBanner()
	{
		if (bannerView != null)
		{
			bannerView.Hide();
		}
	}

	public void RemoveBanner()
	{
		bannerView.Destroy();
	}

	private AdRequest CreateAdmobAdRequest()
	{
		return new AdRequest.Builder().AddTestDevice("SIMULATOR").AddTestDevice("0D47E7F87B327842A29450D6373F3480").Build();
	}

	public string GetTimeSpan()
	{
		DateTime d = Convert.ToDateTime(PlayerPrefs.GetString("last_watch_video")).AddHours(4.0);
		UnityEngine.Debug.Log("Target time:" + d.ToString());
		TimeSpan timeSpan = d - DateTime.Now;
		if (timeSpan.TotalMinutes > 60.0)
		{
			return $"{timeSpan.Hours} hours {timeSpan.Minutes} minutes";
		}
		if (timeSpan.TotalMinutes < 1.0)
		{
			return $"{timeSpan.Seconds} seconds";
		}
		return $"{timeSpan.Minutes} minutes";
	}

	public bool IsRewardVideoAfter4Hours()
	{
		if (!PlayerPrefs.HasKey("last_watch_video"))
		{
			return true;
		}
		DateTime d = Convert.ToDateTime(PlayerPrefs.GetString("last_watch_video"));
		TimeSpan timeSpan = DateTime.Now - d;
		UnityEngine.Debug.Log("Last video time:" + timeSpan.TotalMinutes);
		if (timeSpan.TotalMinutes > 240.0)
		{
			return true;
		}
		return false;
	}

	public bool IsRewardBasedVideoReady()
	{
		return false;
		if (rewardBasedAdmobVideo == null)
		{
			
		}
		return rewardBasedAdmobVideo.IsLoaded();
	}

	public void ShowRewardBasedVideo()
	{
		if (rewardBasedAdmobVideo.IsLoaded())
		{
			rewardBasedAdmobVideo.Show();
			PlayerPrefs.SetString("last_watch_video", DateTime.Now.ToString());
		}
		else
		{
			MonoBehaviour.print("Reward based video ad is not ready yet");
		}
	}

	private void RequestRewardVideo()
	{
		rewardBasedAdmobVideo.LoadAd(CreateAdmobAdRequest(), rewarded_video_ad_unit_id);
	}

	public void RequestInterstitial()
	{
		interstitial = new InterstitialAd(interstitial_ad_unit_id);
		interstitial.OnAdLoaded += HandleOnAdmobAdLoaded;
		interstitial.OnAdFailedToLoad += HandleOnAdmobAdFailedToLoad;
		interstitial.OnAdOpening += HandleOnAdmobAdOpened;
		interstitial.OnAdClosed += HandleOnAdmobAdClosed;
		interstitial.OnAdLeavingApplication += HandleOnAdmobAdLeavingApplication;
		interstitial.LoadAd(CreateAdmobAdRequest());
	}

	public bool IsIntestitialLoaded()
	{
		return false;
		if (interstitial == null)
		{
			return false;
		}
		//return interstitial.IsLoaded();
	}

	public void ShowInterstitial()
	{
		//if (interstitial != null && interstitial.IsLoaded())
		//{
		//	interstitial.Show();
		//}
	}

	public void HandleOnAdmobAdLoaded(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdLoaded event received");
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "interstitial")
		};
		FirebaseAnalytics.LogEvent("ns_ad_received", parameters);
	}

	public void HandleOnAdmobAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
	{
		MonoBehaviour.print("HandleFailedToReceiveAd event received with message: " + args.Message);
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "interstitial")
		};
		FirebaseAnalytics.LogEvent("ns_ad_failed", parameters);
	}

	public void HandleOnAdmobAdOpened(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdOpened event received");
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "interstitial")
		};
		FirebaseAnalytics.LogEvent("ns_ad_impression", parameters);
	}

	public void HandleOnAdmobAdClosed(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdClosed event received");
		RequestInterstitial();
	}

	public void HandleOnAdmobAdLeavingApplication(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdLeftApplication event received");
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "interstitial")
		};
		FirebaseAnalytics.LogEvent("ns_ad_click", parameters);
	}

	public void HandleAdmobRewardBasedVideoLoadedAdmob(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "rewarded")
		};
		FirebaseAnalytics.LogEvent("ns_ad_received", parameters);
	}

	public void HandleAdmobRewardBasedVideoFailedToLoadAdmob(object sender, AdFailedToLoadEventArgs args)
	{
		MonoBehaviour.print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "rewarded")
		};
		FirebaseAnalytics.LogEvent("ns_ad_failed", parameters);
	}

	public void HandleAdmobRewardBasedVideoOpenedAdmob(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "rewarded")
		};
		FirebaseAnalytics.LogEvent("ns_ad_impression", parameters);
	}

	public void HandleAdmobRewardBasedVideoStartedAdmob(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdmobRewardBasedVideoStartedAdmob event received");
	}

	public void HandleAdmobRewardBasedVideoClosedAdmob(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdmobRewardBasedVideoClosedAdmob event received");
		FirebaseStat.Instance.LogSimpleEvent("ns_finish_watch_video");
		rewardBasedAdmobVideo = RewardBasedVideoAd.Instance;
		rewardBasedAdmobVideo.LoadAd(CreateAdmobAdRequest(), rewarded_video_ad_unit_id);
	}

	public void HandleAdmobRewardBasedVideoRewardedAdmob(object sender, Reward args)
	{
		MonoBehaviour.print("HandleAdmobRewardBasedVideoRewardedAdmob event received" + args.ToString());
		FirebaseStat.Instance.LogSimpleEvent("ns_rewarded_video");
		if (DailyEvent.isFreeAd == 1)
		{
			UnityEngine.Object.FindObjectOfType<DailyEvent>().FreeAdsCallBack();
		}
		else
		{
			UnityEngine.Object.FindObjectOfType<ScoreEndGame>().DoubleCoinCallBack();
		}
	}

	public void HandleAdmobRewardBasedVideoLeftApplicationAdmob(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "rewarded")
		};
		FirebaseAnalytics.LogEvent("ns_ad_click", parameters);
	}

	public void HandleAdmobAdLoaded(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdLoaded event received");
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "banner")
		};
		FirebaseAnalytics.LogEvent("ns_ad_received", parameters);
		bannerView.Show();
	}

	public void HandleAdmobAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
	{
		MonoBehaviour.print("HandleFailedToReceiveAd event received with message: " + args.Message);
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "banner")
		};
		FirebaseAnalytics.LogEvent("ns_ad_failed", parameters);
	}

	public void HandleAdmobAdOpened(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdOpened event received");
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "banner")
		};
		FirebaseAnalytics.LogEvent("ns_ad_impression", parameters);
	}

	public void HandleAdmobAdClosed(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdClosed event received");
	}

	public void HandleAdmobAdLeftApplication(object sender, EventArgs args)
	{
		MonoBehaviour.print("HandleAdLeftApplication event received");
		Parameter[] parameters = new Parameter[1]
		{
			new Parameter("ad_unit", "banner")
		};
		FirebaseAnalytics.LogEvent("ns_ad_click", parameters);
	}
}
